package org.db.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.List;

import javax.swing.JDialog;
import javax.swing.SwingUtilities;

import org.db.Manager;
import org.db.dao.GleisDAO;

/**
 * 
 * @author Lutz
 */
public class AuswahlDialog extends CloseCommitDialog
		implements
			ActionListener,
			KeyListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3327635144588703012L;

	public AuswahlDialog(JDialog owner) {
		super(owner);
		setModal(true);
		setTitle("Eingabe");
		initComponents();
	}

	// Initialisierung der GUI- Elemente
	@SuppressWarnings({"rawtypes", "unchecked"})
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		jCheckBoxBauwerk = new javax.swing.JCheckBox();
		jCheckBoxHohenl = new javax.swing.JCheckBox();
		jCheckBoxKrum = new javax.swing.JCheckBox();
		jCheckBoxLage = new javax.swing.JCheckBox();
		jButtonOK = new javax.swing.JButton();
		jButtonBauwerkslinie = new javax.swing.JButton();
		jButtonHoehenlinie = new javax.swing.JButton();
		jButtonKruemmungslinie = new javax.swing.JButton();
		jButtonLageskizze = new javax.swing.JButton();
		jLabel3 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();
		jComboBox1 = new javax.swing.JComboBox();
		jLabelGleisauswahl = new javax.swing.JLabel();
		jLabelAuftragsNr = new javax.swing.JLabel();

		// DialogFrame kann nicht maximiert werden
		setResizable(false);

		// Definition von Eingenschaften der GUI- Elemente
		jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
		jLabel1.setText("Auswahl der Eingabeelemente");

		jButtonOK.setFont(new java.awt.Font("Tahoma", 1, 12));
		jButtonOK.setText("Fertig");
		jButtonOK
				.setToolTipText("Speichern und überprüfen aller Eingaben. Zurück zum Hauptmenü.");
		jButtonOK.addActionListener(this);
		jButtonOK.addKeyListener(this);

		jButtonBauwerkslinie.setFont(new java.awt.Font("Tahoma", 1, 12));
		jButtonBauwerkslinie.setText("Bauwerke");
		jButtonBauwerkslinie.addActionListener(this);
		jButtonBauwerkslinie.addKeyListener(this);

		jButtonHoehenlinie.setFont(new java.awt.Font("Tahoma", 1, 12));
		jButtonHoehenlinie.setText("Höhenlage");
		jButtonHoehenlinie.addActionListener(this);
		jButtonHoehenlinie.addKeyListener(this);

		jButtonKruemmungslinie.setFont(new java.awt.Font("Tahoma", 1, 12));
		jButtonKruemmungslinie.setText("Krümmung/Überhöhung");
		jButtonKruemmungslinie.addActionListener(this);
		jButtonKruemmungslinie.addKeyListener(this);

		jButtonLageskizze.setFont(new java.awt.Font("Tahoma", 1, 12));
		jButtonLageskizze.setText("Lageskizze");
		jButtonLageskizze.addActionListener(this);
		jButtonLageskizze.addKeyListener(this);

		jLabel3.setFont(new java.awt.Font("Tahoma", 3, 11));
		jLabel3.setText("");

		jLabel4.setFont(new java.awt.Font("Tahoma", 3, 11));
		jLabel4.setText("");

		jCheckBoxBauwerk.setEnabled(false);
		jCheckBoxHohenl.setEnabled(false);
		jCheckBoxKrum.setEnabled(false);
		jCheckBoxLage.setEnabled(false);

		// Laden der Gleise
		List<GleisDAO> gleise = Manager.getInstance().getGleisElemente();
		jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(
				(GleisDAO[]) gleise.toArray(new GleisDAO[gleise.size()])));
		Manager.getInstance().setCurrentGleis(gleise.get(0));
		jComboBox1.addItemListener(new ItemListener() {

			@Override
			public void itemStateChanged(ItemEvent e) {
				Manager.getInstance().setCurrentGleis(
						(GleisDAO) jComboBox1.getSelectedItem());
				updateCheckBoxes();

			}
		});

		jLabelGleisauswahl.setFont(new java.awt.Font("Tahoma", 1, 12));
		jLabelGleisauswahl.setText("Gleis");

		jLabelAuftragsNr.setFont(new java.awt.Font("Tahoma", 3, 12));
		jLabelAuftragsNr.setText(Manager.getInstance().getProjekt()
				.getAuftragsNr());

		// horizontale und vertikale Ausrichtung der GUI- Komponenten auf dem
		// DialogFrame
		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addContainerGap()
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addGroup(
																						javax.swing.GroupLayout.Alignment.TRAILING,
																						layout.createSequentialGroup()
																								.addGroup(
																										layout.createParallelGroup(
																												javax.swing.GroupLayout.Alignment.LEADING)
																												.addGroup(
																														javax.swing.GroupLayout.Alignment.TRAILING,
																														layout.createSequentialGroup()
																																.addComponent(
																																		jButtonBauwerkslinie,
																																		javax.swing.GroupLayout.DEFAULT_SIZE,
																																		183,
																																		Short.MAX_VALUE)
																																.addPreferredGap(
																																		javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																																.addComponent(
																																		jCheckBoxBauwerk)
																																.addGap(362,
																																		362,
																																		362))
																												.addGroup(
																														layout.createSequentialGroup()
																																.addComponent(
																																		jLabel1,
																																		javax.swing.GroupLayout.DEFAULT_SIZE,
																																		javax.swing.GroupLayout.DEFAULT_SIZE,
																																		Short.MAX_VALUE)
																																.addGap(298,
																																		298,
																																		298)))
																								.addGap(117,
																										117,
																										117))
																				.addGroup(
																						layout.createSequentialGroup()
																								.addGroup(
																										layout.createParallelGroup(
																												javax.swing.GroupLayout.Alignment.LEADING,
																												false)
																												.addComponent(
																														jButtonHoehenlinie,
																														javax.swing.GroupLayout.Alignment.TRAILING,
																														javax.swing.GroupLayout.DEFAULT_SIZE,
																														javax.swing.GroupLayout.DEFAULT_SIZE,
																														Short.MAX_VALUE)
																												.addComponent(
																														jButtonKruemmungslinie,
																														javax.swing.GroupLayout.Alignment.TRAILING,
																														javax.swing.GroupLayout.DEFAULT_SIZE,
																														183,
																														Short.MAX_VALUE)
																												.addComponent(
																														jButtonLageskizze,
																														javax.swing.GroupLayout.DEFAULT_SIZE,
																														javax.swing.GroupLayout.DEFAULT_SIZE,
																														Short.MAX_VALUE))
																								.addPreferredGap(
																										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																								.addGroup(
																										layout.createParallelGroup(
																												javax.swing.GroupLayout.Alignment.LEADING,
																												false)
																												.addGroup(
																														layout.createSequentialGroup()
																																.addComponent(
																																		jCheckBoxLage)
																																.addPreferredGap(
																																		javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																																.addComponent(
																																		jLabel4,
																																		javax.swing.GroupLayout.DEFAULT_SIZE,
																																		javax.swing.GroupLayout.DEFAULT_SIZE,
																																		Short.MAX_VALUE))
																												.addComponent(
																														jCheckBoxKrum)
																												.addGroup(
																														layout.createSequentialGroup()
																																.addComponent(
																																		jCheckBoxHohenl)
																																.addPreferredGap(
																																		javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																																.addGroup(
																																		layout.createParallelGroup(
																																				javax.swing.GroupLayout.Alignment.LEADING)
																																				.addGroup(
																																						layout.createSequentialGroup()
																																								.addComponent(
																																										jLabelGleisauswahl)
																																								.addPreferredGap(
																																										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																																								.addComponent(
																																										jComboBox1,
																																										javax.swing.GroupLayout.PREFERRED_SIZE,
																																										javax.swing.GroupLayout.DEFAULT_SIZE,
																																										javax.swing.GroupLayout.PREFERRED_SIZE))
																																				.addComponent(
																																						jLabel3,
																																						javax.swing.GroupLayout.PREFERRED_SIZE,
																																						80,
																																						javax.swing.GroupLayout.PREFERRED_SIZE)))))))
												.addGroup(
														layout.createSequentialGroup()
																.addContainerGap()
																.addComponent(
																		jLabelAuftragsNr,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		117,
																		javax.swing.GroupLayout.PREFERRED_SIZE))
												.addGroup(
														javax.swing.GroupLayout.Alignment.TRAILING,
														layout.createSequentialGroup()
																.addContainerGap(
																		582,
																		Short.MAX_VALUE)
																.addComponent(
																		jButtonOK,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		117,
																		javax.swing.GroupLayout.PREFERRED_SIZE)))
								.addContainerGap()));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(jLabel1)
								.addGap(11, 11, 11)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabelAuftragsNr)
												.addComponent(
														jLabelGleisauswahl)
												.addComponent(
														jComboBox1,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.TRAILING)
												.addGroup(
														layout.createSequentialGroup()
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.TRAILING)
																				.addComponent(
																						jButtonBauwerkslinie,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						32,
																						javax.swing.GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						jCheckBoxBauwerk))
																.addGap(18, 18,
																		18)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.TRAILING)
																				.addComponent(
																						jCheckBoxHohenl)
																				.addComponent(
																						jButtonHoehenlinie,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						32,
																						javax.swing.GroupLayout.PREFERRED_SIZE)))
												.addComponent(jLabel3))
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.TRAILING)
												.addGroup(
														layout.createSequentialGroup()
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.TRAILING)
																				.addComponent(
																						jCheckBoxKrum)
																				.addComponent(
																						jButtonKruemmungslinie,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						32,
																						javax.swing.GroupLayout.PREFERRED_SIZE))
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addGroup(
																						layout.createSequentialGroup()
																								.addGap(18,
																										18,
																										18)
																								.addComponent(
																										jButtonLageskizze,
																										javax.swing.GroupLayout.PREFERRED_SIZE,
																										32,
																										javax.swing.GroupLayout.PREFERRED_SIZE))
																				.addGroup(
																						layout.createSequentialGroup()
																								.addGap(29,
																										29,
																										29)
																								.addComponent(
																										jCheckBoxLage))))
												.addComponent(jLabel4))
								.addGap(49, 49, 49)
								.addComponent(jButtonOK,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										34,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addContainerGap()));

		updateCheckBoxes();
		pack();
		setLocationRelativeTo(null);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == jButtonBauwerkslinie) {
			final BauwerkslinienDialog viewer = new BauwerkslinienDialog(this);
			SwingUtilities.invokeLater(new Runnable() {
				@Override
				public void run() {
					viewer.setLocationRelativeTo(null);
					@SuppressWarnings("unused")
					int returnCode = viewer.showBauwerkslinienDialog();
					updateCheckBoxes();
					// if(returnCode == JOptionPane.OK_OPTION){
					// jCheckBoxBauwerk.setSelected(true);
					// jCheckBoxBauwerk.setEnabled(false);
					// }
				}
			});
		} else if (e.getSource() == jButtonHoehenlinie) {
			final HoehenlinienDialog viewer = new HoehenlinienDialog(this);
			SwingUtilities.invokeLater(new Runnable() {
				@SuppressWarnings("unused")
				@Override
				public void run() {
					viewer.setLocationRelativeTo(null);
					int returnCode = viewer.showHoehenlinienDialog();
					updateCheckBoxes();
					// if(returnCode == JOptionPane.OK_OPTION){
					// jCheckBoxHohenl.setSelected(true);
					// jCheckBoxHohenl.setEnabled(false);
					// }
				}
			});
		} else if (e.getSource() == jButtonKruemmungslinie) {
			final KruemmungslinienDialog viewer = new KruemmungslinienDialog(
					this);
			SwingUtilities.invokeLater(new Runnable() {
				@SuppressWarnings("unused")
				@Override
				public void run() {
					viewer.setLocationRelativeTo(null);
					int returnCode = viewer.showKruemmungslinienDialog();
					updateCheckBoxes();
					// if(returnCode == JOptionPane.OK_OPTION){
					// jCheckBoxKrum.setSelected(true);
					// jCheckBoxKrum.setEnabled(false);
					// }
				}
			});
		} else if (e.getSource() == jButtonLageskizze) {
			final LageskizzenDialog viewer = new LageskizzenDialog(this);
			SwingUtilities.invokeLater(new Runnable() {
				@SuppressWarnings("unused")
				@Override
				public void run() {
					viewer.setLocationRelativeTo(null);
					int returnCode = viewer.showLageskizzenDialog();
					updateCheckBoxes();
					// if(returnCode == JOptionPane.OK_OPTION){
					// jCheckBoxLage.setSelected(true);
					// jCheckBoxLage.setEnabled(false);
					// }
				}
			});
		} else if (e.getSource() == jButtonOK) {
			setVisible(false);
			dispose();
		}
	}

	/**
	 * Aktivierung der jeweiligen Checkbox, wenn gespeicherte Daten vorliegen
	 */
	private void updateCheckBoxes() {
		boolean existsHoehenlinienElement = !Manager.getInstance()
				.getHoehenlinienElemente().isEmpty();
		jCheckBoxHohenl.setSelected(existsHoehenlinienElement);

		boolean existsKruemmungsOrUeberhoehungslinienElement = !Manager
				.getInstance().getKruemmungslinienElemente().isEmpty()
				|| !Manager.getInstance().getUeberhoehungslinienElemente()
						.isEmpty();
		jCheckBoxKrum.setSelected(existsKruemmungsOrUeberhoehungslinienElement);

		boolean existsLageskizzenElement = !Manager.getInstance()
				.getLageskizzenGVPs().isEmpty();
		jCheckBoxLage.setSelected(existsLageskizzenElement);

		boolean existsBauwerkslinienElement = !Manager.getInstance()
				.getBauwerkslinienElemente().isEmpty();
		jCheckBoxBauwerk.setSelected(existsBauwerkslinienElement);
	}

	// Deklaration der Variabeln
	private javax.swing.JButton jButtonBauwerkslinie;

	private javax.swing.JButton jButtonHoehenlinie;

	private javax.swing.JButton jButtonKruemmungslinie;

	private javax.swing.JButton jButtonLageskizze;

	private javax.swing.JButton jButtonOK;

	private javax.swing.JCheckBox jCheckBoxBauwerk;

	private javax.swing.JCheckBox jCheckBoxHohenl;

	private javax.swing.JCheckBox jCheckBoxKrum;

	private javax.swing.JCheckBox jCheckBoxLage;

	@SuppressWarnings("rawtypes")
	private javax.swing.JComboBox jComboBox1;

	private javax.swing.JLabel jLabel1;

	private javax.swing.JLabel jLabel3;

	private javax.swing.JLabel jLabel4;

	private javax.swing.JLabel jLabelAuftragsNr;

	private javax.swing.JLabel jLabelGleisauswahl;

	@Override
	public void keyPressed(KeyEvent e) {
		if (e.getSource() == jButtonOK) {
			if (e.getKeyCode() == KeyEvent.VK_ENTER) {
				jButtonOK.doClick();
			}
		} else if (e.getSource() == jButtonHoehenlinie) {
			if (e.getKeyCode() == KeyEvent.VK_ENTER) {
				jButtonHoehenlinie.doClick();
			}
		} else if (e.getSource() == jButtonKruemmungslinie) {
			if (e.getKeyCode() == KeyEvent.VK_ENTER) {
				jButtonKruemmungslinie.doClick();
			}
		} else if (e.getSource() == jButtonLageskizze) {
			if (e.getKeyCode() == KeyEvent.VK_ENTER) {
				jButtonLageskizze.doClick();
			}
		} else if (e.getSource() == jButtonBauwerkslinie) {
			if (e.getKeyCode() == KeyEvent.VK_ENTER) {
				jButtonBauwerkslinie.doClick();
			}
		}

	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

}
